package com.br.pfm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.br.pfm.persistence.config.JPAConfig;

@Configuration
@ComponentScan("com.br.pfm")
@EnableWebMvc
@Import(JPAConfig.class)
public class ApplicationContext extends WebMvcConfigurerAdapter {
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/css/**").addResourceLocations("resources/css/");
		registry.addResourceHandler("/resources/fonts/**").addResourceLocations("resources/fonts/");
		registry.addResourceHandler("/resources/img/**").addResourceLocations("resources/img/");
		registry.addResourceHandler("/resources/js/**").addResourceLocations("resources/js/");
	}
	
	@Bean
    public ViewResolver getViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
	
	@Bean
	public static PropertySourcesPlaceholderConfigurer properties() {
		final PropertySourcesPlaceholderConfigurer propertySources = new PropertySourcesPlaceholderConfigurer();
		final Resource[] resources = new ClassPathResource[] { new ClassPathResource("spring.properties") };
		propertySources.setLocations(resources);
		propertySources.setIgnoreUnresolvablePlaceholders(true);
		propertySources.setFileEncoding("UTF-8");
		return propertySources;
	}
	
	@Bean
	public ReloadableResourceBundleMessageSource messageSource() {
		final ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
		source.setBasename("WEB-INF/messages/messages");
		source.setDefaultEncoding("UTF-8");
		return source;
	}
	
	@Bean
	public LocaleResolver localeResolver() {
		return new org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver();
	}

}
